<?php


namespace app\api\controller\xilujob;

use app\common\controller\xilujob\XilujobApi;
use think\Db;
use think\Exception;

class CompanyComment extends XilujobApi {

    protected $noNeedLogin = ['lists'];
    protected $noNeedRight = ['*'];
    protected $restrictedActions = [
    ];

    /**
     * 发布评论
     */
    public function add_comment() {
        $params = $this->request->post('',null,'trim');
        $params['user_id'] = $this->auth->id;
        Db::startTrans();
        try {
            $ret = \app\common\model\xilujob\CompanyComment::addComment($params);
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        Db::commit();
        $this->success('评论成功', $ret);

    }

    /**
     * 我发布的评论
     */
    public function my_comment() {
        $list = \app\common\model\xilujob\CompanyComment::where('company_comment.user_id', $this->auth->id)
            ->with([
                'user'=>function($query){$query->withField('id,avatar,nickname');},
                'enterpriseinfo'=>function($query){$query->withField('id,full_name,company_name');},
            ])
            ->order('id','desc')
            ->paginate();
        $this->success('',['company_comments'=>$list->getCollection()]);
    }

    public function lists($company_id) {
        $list = \app\common\model\xilujob\CompanyComment::where('company_comment.company_id',$company_id)
            ->with([
                'user'=>function($query){$query->withField('id,avatar,nickname');},
                'enterpriseinfo'=>function($query){$query->withField('id,full_name,company_name');},
            ])
            //->where('company_comment.status','1')
            ->order('id','desc')
            ->paginate();
        $this->success('', ['company_comments'=>$list->getCollection(), 'total'=>$list->total()]);

    }

}